﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Education
{
	public partial class MemberExamSearch : MemberBasePage
	{
		private int MID
		{
			get
			{
				return UserInfo.MemberID;
			}
		}
		public int PageFlag { get; set; }

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				MemberName();
				string Sel = this.SelExam.SelectedValue;
				BindSel(Sel);
				BindData(1, Sel);
			}
			else
			{
				BindDropDownList();
			}
		}

		protected void BindData(int pageIndex, string Sel)
		{
			this.PageFlag = (pageIndex - 1) * this.Pager1.PageSize;
			int records = 0;
			Expression<Func<Exam_Memeber_Quota, bool>> f;
			if (string.IsNullOrWhiteSpace(Sel))
			{
				if (this.MID == -1)
				{
					f = (p => p.Status == 0);
				}
				else
				{
					f = (p => p.MemberID == MID && p.Status == 0);
				}
			}
			else
			{
				if (this.MID == -1)
				{
					f = (p => p.Status == 0 && p.Subject == Sel);
				}
				else
				{
					f = (p => p.MemberID == MID && p.Status == 0 && p.Subject == Sel);
				}
			}

			Expression<Func<Exam_Memeber_Quota, DateTime>> order = (c => c.Deadline);
			var resultQ = DAL.DALHelper.Instance.GetList<Exam_Memeber_Quota, DateTime>(f, order, false, this.Pager1.PageSize, pageIndex, out records);

			this.List.DataSource = resultQ;
			this.List.DataBind();
			this.Pager1.RecordCount = records;
			this.Pager1.InitControl();
		}

		protected void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
		{
			string Sel = this.SelExam.SelectedValue;
			BindData(nextPage, Sel);
		}

		protected void BindDropDownList()
		{
			string Sel = this.SelExam.SelectedValue;
			BindData(1, Sel);
		}

		protected void BindSel(string SelD)
		{
			Expression<Func<Exam_Memeber_Quota, bool>> f;
			if (MID == -1)
			{
				f = (p => p.Status == 0);
			}
			else
			{
				f = (p => p.MemberID == MID && p.Status == 0);
			}
			Expression<Func<Exam_Memeber_Quota, DateTime>> order = (c => c.Deadline);
			var resultQ = DAL.DALHelper.Instance.GetList<Exam_Memeber_Quota, DateTime>(f, order, false)
				.Select(d => (new { name = d.Subject })).Distinct();
			this.SelExam.DataSource = resultQ;
			this.SelExam.DataTextField = "name";
			this.SelExam.DataValueField = "name";
			this.SelExam.DataBind();
		}

		protected void MemberName()
		{
			var resultM = DAL.DALHelper.Instance.GetObject<TB_Member>("ID", this.MID);
			if (resultM != null)
			{
				this.MemberN.Text = (String.IsNullOrWhiteSpace(resultM.Name.Trim()) ? "&lt;N/A&gt;" : resultM.Name);
			}
			else
			{
				this.MemberN.Text = "&lt;N/A&gt;";
			}
		}
	}
}